package cn.teamwork.ssm.service;

import cn.teamwork.ssm.mapper.UserMapper;
import cn.teamwork.ssm.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service

public class UserService {
    @Autowired
    private UserMapper mapper;

    public Map<String,Object> list(Integer page,Integer size){

        int limit = size == null || size <1?10:size;

        page = page  == null || page<1?1:page;

        int offset=(page-1)*limit;

        List<User> users = mapper.find(offset, limit);

        Long count = mapper.count();

        Map<String,Object> map=new HashMap<>();
        map.put("users",users);
        map.put("count",count);
        return map;
    }
    public Long checkUsernameAndPassword(String username,String password){
        if(!mapper.exists(username)){
            return -1L;
        }
        Long id=mapper.findIDByUsernameAndPassword(username,password);
        if(id==null||id<1){
            return -2L;
        }
        if(mapper.isDelected(id)){
            return -3L;
        }
        if(mapper.isDisabled(id)){
            return -4L;
        }
        return id;
    }
    public Long checkUser(String username, String name){
        if (mapper.exists(username)){
            return -1L;
        }else {
            mapper.addUser(username, name);
        }
        return 1L;
    }
}